
import os
from PIL import Image
import torch.utils.data as data
import torchvision.transforms as transforms
import numpy as np
import torch
import numpy as np

import cv2


# 设置源文件夹和目标文件夹路径
path_A = r'/media/jolly/HIK/THE FLAME DATASET/label/1/'
path_B = r'/media/jolly/HIK/THE FLAME DATASET/label/predictResult'

# 遍历A文件夹中的所有jpg文件
for filename in os.listdir(path_A):
    if filename.endswith('.png'):
        # 读取图片
        img = cv2.imdecode(np.fromfile(path_A + filename,dtype=np.uint8),cv2.IMREAD_GRAYSCALE)
        # 将像素值为256的转成1
        img = cv2.resize(img, (256, 256))
        img[img == 1] = 255
        # 将图片文件名和后缀分离
        name, ext = os.path.splitext(filename)
        # 保存成png格式
        cv2.imencode(".png",img)[1].tofile(os.path.join(path_B,filename))

# import os
# import shutil
#
# # A文件夹路径
# path_a = r"E:/医学/胃肠镜/Kvasir-SEG/val/image"
# # B文件夹路径
# path_b = r'E:\医学\胃肠镜\Kvasir-SEG\train\masks'
# # C文件夹路径
# path_c = r'E:\医学\胃肠镜\Kvasir-SEG\val\label'
#
# # 遍历A文件夹
# for file in os.listdir(path_a):
#     # 判断B文件夹是否有同名文件
#     if os.path.exists(os.path.join(path_b, file)):
#         # 拷贝B文件夹中的同名文件到C文件夹中
#         shutil.copy(os.path.join(path_b, file), os.path.join(path_c, file))

# 将代码中的“/path/to/A”、“/path/to/B”和“/path/to/C”分别替换成你实际的文件夹路径。这段代码将会遍历A文件夹中的所有文件，如果在B文件夹中存在同名文件，则会将该文件从B文件夹拷贝到C文件夹。

# for file in os.listdir(path):
#     if "_1" in file  or "_2" in file  or "_3" in file:
#         os.remove(os.path.join(path, file))


# c=np.arange(72).reshape(2,2,6,3)
# index=np.random.randint(0,3,size=(2,2,6,1))
#
# c=torch.tensor(c)
# index=torch.tensor(index,dtype=torch.int64)
# index=torch.tile(index,(1,1,1,3))
# out=torch.gather(c,dim=-2,index=index)
# print(out)
'''
gt_root="D:\\Massachusetts\\train\\label1\\"

t=[gt_root + f for f in os.listdir(gt_root) if f.endswith('.png') or f.endswith('.tif')]



data=np.fromfile("D:\\Massachusetts\\train\\label1\\22678915_15.png",dtype=np.uint8)
img= cv2.imdecode(data,0 )
img[0,0]=1
print(img)
ten= torch.tensor(img)
print(ten)


with open("D:\\Massachusetts\\train\\label1\\22678915_15.png", 'rb') as f:



            img = Image.open(f)
            img.show()
            # return img.convert('1')
            img.convert('L')
            ten= transforms.ToTensor()(img)
            print(ten)
'''